package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 新增菜品
     *
     * @param dto
     */
    @Transactional
    public void insertDish(DishDTO dto) {
        Dish dish = new Dish();
        //属性拷贝
        BeanUtils.copyProperties(dto, dish);
        //设置菜品状态
        dish.setStatus(StatusConstant.DISABLE);
        dishMapper.insertDish(dish);
        Long dishId = dish.getId();

        List<DishFlavor> flavors = dto.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(flavor -> flavor.setDishId(dishId));
            dishFlavorMapper.insertBatch(flavors);
        }


    }

    /**
     * 分页查询菜品
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dto);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除菜品
     *
     * @param ids
     */
    @Transactional
    @Override
    public void delete(List<Long> ids) {
        //判断当前菜品是否存在起售中的状态
        Integer countById = dishMapper.getCountById(ids);

        if (countById>0) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }

        //判断是否被套餐关联了
        Integer countByDishIds = setmealDishMapper.getCountByDishIds(ids);
        if (countByDishIds>0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
            //删除表中数据
            dishMapper.delete(ids);
            //删除菜品关联的口味数据
            dishFlavorMapper.deleteByDishIds(ids);
    }

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    @Override
    public DishVO selectDishById(Long id) {
        return dishMapper.selectDishById(id);
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> selectCategoryById(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.selectCategoryById(dish);
    }

    /**
     * 根据菜品id修改菜品的状态
     *
     * @param status
     * @param id
     */
    @Override
    public void UpdateStatus(Integer status, Long id) {
        Dish dish = Dish.builder().id(id).status(status).build();
        dishMapper.updateDish(dish);
    }

    /**
     * 根据菜品id更新菜品
     * @param dishDTO
     */
    @Override
    public void updateDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        //对象属性拷贝
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.updateDish(dish);

        List<Long> ids = Arrays.asList(dishDTO.getId());
        //删除原有的口味数据
        dishFlavorMapper.deleteByDishIds(ids);
        //获取新的口味数据并插入到口味表中
        if (CollectionUtils.isNotEmpty(dishDTO.getFlavors())) {
            // 给口味信息赋值
            // 赋值 口味信息的id
            for (DishFlavor flavor : dishDTO.getFlavors()) {
                flavor.setDishId(dishDTO.getId());
            }
            // 写入到数据库中
            this.dishFlavorMapper.insertBatch(dishDTO.getFlavors());
        }
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.selectCategoryById(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
